package com.jrx.anytxn.param.transaction.job.paph.config;

import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.entity.TlFilePaymentLog;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.param.transaction.config.AnytxnBatchConfigProperties;
import com.jrx.anytxn.param.transaction.job.paph.step.UpdateFileLogProcessor;
import com.jrx.anytxn.param.transaction.job.paph.step.UpdateFileLogWriter;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.support.SynchronizedItemStreamReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 更新文件还款流水
 * @author  jing.xy
 */
@Configuration
public class UpdateFileLogConfiguration {

    Logger logger = LoggerFactory.getLogger(UpdateFileLogConfiguration.class);

    @Resource(name = "paramSqlSessionFactory")
    SqlSessionFactory paramSqlSessionFactory;
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private AnytxnBatchConfigProperties anytxnBatchConfigProperties;
    @Resource
    private UpdateFileLogProcessor updateFileLogProcessor;
    @Resource
    private UpdateFileLogWriter updateFileLogWriter;
    @Autowired
    private IOrganizationTableService organizationTableService;
    @Autowired
    private PlatformTransactionManager platformTransactionManager;



    @Bean
    public Job updateFileLogJob() {
        return jobBuilderFactory.get("updateFileLogJob")
                .start(updateFileLogStep())
                .build();
    }

    @Bean
    public Step updateFileLogStep() {
        return stepBuilderFactory.get("updateFileLogStep")
                .<TlFilePaymentLog, TlFilePaymentLog>chunk(anytxnBatchConfigProperties.getFileRepayConfig().getChunkLimit())//表示一批处理100条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(updateFileLogReader())
                .processor(updateFileLogProcessor)
                .writer(updateFileLogWriter)
                .transactionManager(platformTransactionManager)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchConfigProperties.getFileRepayConfig().getThrottleLimit())//多线程
                .build();
    }

    @Bean
    @StepScope
    public SynchronizedItemStreamReader<TlFilePaymentLog> updateFileLogReader() {
        MyBatisCursorItemReader<TlFilePaymentLog> cursorItemReader = new MyBatisCursorItemReader<>();
        cursorItemReader.setSaveState(false);
        cursorItemReader.setSqlSessionFactory(paramSqlSessionFactory);
        cursorItemReader.setQueryId("com.jrx.anytxn.param.mapper.ext.ExtTlFilePaymentLogMapper.selectPaymentLogForBatch");
        Map<String, Object> map = new HashMap<>();

        PrOrganizationTable organizationTable = organizationTableService.findByOrgIdForBatch(TransBizConstant.DEFAULT_ORG_ID);
        map.put("tradeDate",organizationTable.getNextProcessingDate());
        map.put("tradeFlagList", Arrays.asList(TransBizConstant.FILE_TRADE_FLAG_0,TransBizConstant.FILE_TRADE_FLAG_1,TransBizConstant.FILE_TRADE_FLAG_2));

        cursorItemReader.setParameterValues(map);
        SynchronizedItemStreamReader<TlFilePaymentLog> synchronizedItemStreamReader = new SynchronizedItemStreamReader<>();
        synchronizedItemStreamReader.setDelegate(cursorItemReader);
        return synchronizedItemStreamReader;
    }


}
